package com.witdata.common.service.course.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.mybatisflex.core.query.QueryWrapper;
import com.witdata.common.domain.Status;
import com.witdata.common.domain.vo.CourseSubjectsVo;
import com.witdata.common.exception.GlobalException;
import com.witdata.common.service.course.CourseSubjectsService;
import com.witdata.mysql.base.service.CourseSubjectsBaseService;
import com.witdata.mysql.domain.entity.TCourseSubjects;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class CourseSubjectsServiceImpl implements CourseSubjectsService {
    
    private final CourseSubjectsBaseService courseSubjectsBaseService;

    @Override
    public List<CourseSubjectsVo> getCourseSubjectsList(Long courseId) {
        // 判断课程是否存在
        if (ObjectUtils.isEmpty(courseId)) throw new GlobalException(Status.PARAMERROR);
        Boolean exists = courseSubjectsBaseService.exists(new QueryWrapper().eq("course_id", courseId));
        if (!exists) throw new GlobalException(Status.COURSENOTFOUND);
        // 查询课程题单列表
        List<TCourseSubjects> courseSubjectsList = courseSubjectsBaseService.list(new QueryWrapper().eq("course_id", courseId));
        return courseSubjectsList.stream().map(CourseSubjectsVo::new).collect(Collectors.toList());
    }

    @Override
    public Boolean createCourseSubjects(CourseSubjectsVo courseSubjectsVo) {
        return courseSubjectsBaseService.save(courseSubjectsVo.toTCourseSubjects());
    }

    @Override
    public Boolean updateCourseSubjects(CourseSubjectsVo courseSubjectsVo) {
        // 判断课程题单是否存在
        if (ObjectUtils.isEmpty(courseSubjectsVo.getId())) throw new GlobalException(Status.PARAMERROR);
        Boolean exists = courseSubjectsBaseService.exists(new QueryWrapper().eq("id", courseSubjectsVo.getId()));
        if (!exists) throw new GlobalException(Status.COURSE_SUBJECTS_NOT_FOUND);
        // 更新课程题单
        return courseSubjectsBaseService.updateById(courseSubjectsVo.toTCourseSubjects());
    }

    @Override
    public Boolean deleteCourseSubjects(Long courseSubjectsId) {
        // 判断课程题单是否存在
        if (ObjectUtils.isEmpty(courseSubjectsId)) throw new GlobalException(Status.PARAMERROR);
        Boolean exists = courseSubjectsBaseService.exists(new QueryWrapper().eq("id", courseSubjectsId));
        if (!exists) throw new GlobalException(Status.COURSE_SUBJECTS_NOT_FOUND);
        // 删除课程题单
        return courseSubjectsBaseService.removeById(courseSubjectsId);
    }
}
